package com.ch.boundarycallbackdemo

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.ch.boundarycallbackdemo.adapter.NewsAdapter
import com.ch.boundarycallbackdemo.bean.Data
import com.ch.boundarycallbackdemo.viewmodel.NewsViewModel

class MainActivity : AppCompatActivity() {
    private lateinit var newsViewModel: NewsViewModel
    private lateinit var adapter: NewsAdapter
    private lateinit var recyclerView: RecyclerView
    private val config = object : DiffUtil.ItemCallback<Data>() {
        override fun areItemsTheSame(oldItem: Data, newItem: Data): Boolean {
            return oldItem == newItem
        }

        override fun areContentsTheSame(oldItem: Data, newItem: Data): Boolean {
            return oldItem == newItem
        }


    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        recyclerView = findViewById(R.id.recycle_view)
        recyclerView.layoutManager = LinearLayoutManager(this)
        adapter = NewsAdapter(config);
        recyclerView.adapter = adapter

        newsViewModel = ViewModelProviders.of(this).get(NewsViewModel::class.java)

        newsViewModel.newsList.observe(this) {
            adapter.submitList(it)
        }

        newsViewModel.stateBean.observe(this) {
            Log.e("Hogan_cpr_gg", "${it.code}    ${it.result}")
        }

    }


}

